package com.windward.vii.utils;

import java.math.BigDecimal;

/**
 * 提供浮点数计算的工具方法
 * @author Chris
 *
 */
public class DoubleUtil {
	
	/**
	 * 两个浮点数相乘
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static double multiply(double value1, double value2){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		return b1.multiply(b2).doubleValue();
	}
	public static double multiply(double value1, double value2, double value3){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		BigDecimal b3 = new BigDecimal(String.valueOf(value3));
		return b1.multiply(b2).multiply(b3).doubleValue();
	}
	public static double multiply(double value1, float value2){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		return b1.multiply(b2).doubleValue();
	}
	public static double multiply(double value1, int value2){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		return b1.multiply(b2).doubleValue();
	}
	public static  double add(double value1, double value2){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		return b1.add(b2).doubleValue();
	}
	public static double divide(double value1, double value2){
		BigDecimal b1 = new BigDecimal( String.valueOf(value1));
		BigDecimal b2 = new BigDecimal( String.valueOf(value2));
		if(b2.compareTo(new BigDecimal(0))==0){
			return 0;
		}
		return b1.divide(b2,4,BigDecimal.ROUND_HALF_DOWN).doubleValue();
	}
	public static double divide(double value1, double value2, double value3){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		BigDecimal b3 = new BigDecimal(String.valueOf(value3));
		if(b2.compareTo(new BigDecimal(0))==0){
			return 0;
		}
		if(b3.compareTo(new BigDecimal(0))==0){
			return 0;
		}
		return b1.divide(b2,4,BigDecimal.ROUND_HALF_DOWN).divide(b3,4,BigDecimal.ROUND_HALF_DOWN).doubleValue();
	}
	public static double sub(double value1, double value2){
		BigDecimal b1 = new BigDecimal(String.valueOf(value1));
		BigDecimal b2 = new BigDecimal(String.valueOf(value2));
		return b1.subtract(b2).doubleValue();
	}
	public static double remainder(double value1, double value2, int scale){
        if (scale < 0) { 
            throw new IllegalArgumentException("The scale must be a positive integer or zero"); 
        } 
        BigDecimal b1 = new BigDecimal(String.valueOf(value1)); 
        BigDecimal b2 = new BigDecimal(String.valueOf(value2)); 
        return b1.remainder(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
	/**
	 * 执行两个浮点数的比较
	 * 支持精度为4位小数
	 * @param a
	 * @param b
	 * @return  -1 a 小于b , 0 a 等于 b,   a大于 b
	 * @author xwarrior 
	 * @since 2013/12/16
	 */
	public static int compare(double a,double b){
		double diff = a - b;
		Long longdiff = Math.round(diff * 10000f);
		return longdiff.compareTo(0L);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double value1 = 3.0;
		double value2 = 0.3;
		
		System.out.println(multiply(value1, value2));
		System.out.println(add(value1, value2));
		System.out.println(divide(value1, 0));
		System.out.println(sub(value1, value2));
		System.out.println(remainder(value1, value2,0));
		System.out.println(compare(remainder(value1, value2,2),0.0d));
		
		System.out.println("0.0f == 0.0f:" + compare(0f,0f) );
		System.out.println("-0.1345 < 0.0:" + compare(-0.1345,0.0) );
		System.out.println("-0.1345 > -0.1356:" + compare(-0.1345,-0.1356) );
		System.out.println("1.0003 < 1.0004:" + compare(1.0003f,1.0004f) );
		System.out.println("100.2403 < 100.2504:" + compare(100.2503,100.2504) );
		System.out.println("100.2403 == 100.2403:" + compare(100.2403,100.2403) );
		System.out.println("100.2403 > 100.2302:" + compare(100.2403,100.2502) );
	}

}
